package com.dareway.thread;

public class Thread_yield {
    //使用yield()方法来将当前资源让位给其他同优先级线程：
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            System.out.println("线程1开始执行");
            for (int i = 0; i < 50; i ++ ) {
                if (i % 5 == 0) {
                    System.out.println("让位");
                    Thread.yield();
                }
                System.out.println("1：" + i);
            }
            System.out.println("线程1开始执行");
        });
        Thread t2 = new Thread(()->{
            System.out.println("线程2开始执行");
            for (int i = 0; i < 50; i ++ ) {
                System.out.println("2：" + i);
            }
            System.out.println("线程2开始执行");
        });
        t1.start();
        t2.start();
    }
}
